Imports System.Windows.Forms
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree
Imports Vintasoft.Imaging

Namespace Pdf
	Public Partial Class EditBookmarkNodeForm
		Inherits Form

		#Region "Fields"

		Private _document As PdfDocument
		Private _viewer As ImageViewerBase
		Private _action As PdfAction
		Private _init As Boolean = True

		#End Region


		#Region "Constructor"

		Public Sub New(initalPageIndex As Integer, initalOutline As PdfBookmark, add As Boolean)
			Me.New(Nothing, initalPageIndex, initalOutline, add)
		End Sub

		Public Sub New(viewer As ImageViewerBase, initalPageIndex As Integer, initalOutline As PdfBookmark, add As Boolean)
			InitializeComponent()
			_viewer = viewer
			addToRootCheckBox.Visible = add
			_outline = initalOutline
			_document = initalOutline.Document
			If _viewer Is Nothing Then
				pageNumber.Maximum = _document.Pages.Count
			Else
				pageNumber.Maximum = _viewer.Images.Count
			End If
			If initalPageIndex < 0 Then
				initalPageIndex = 0
			End If
			pageNumber.Value = initalPageIndex + 1
			outlineTitle.Text = _outline.Title
			outlineExpanded.Checked = _outline.IsExpanded
			outlineBold.Checked = (_outline.Flags And PdfBookmarkFlags.Bold) <> 0
			outlineItalic.Checked = (_outline.Flags And PdfBookmarkFlags.Italic) <> 0
			colorDialog1.Color = _outline.Color
			_action = _outline.Action
			If _action IsNot Nothing AndAlso _outline.Destination Is Nothing Then
				actionRadioButton.Checked = True
			Else
				destRadioButton.Checked = True
			End If
			_init = False
		End Sub

		#End Region


		#Region "Properties"

		Private _outline As PdfBookmark
		Public ReadOnly Property Bookmark() As PdfBookmark
			Get
				Return _outline
			End Get
		End Property

		Public ReadOnly Property AddToRoot() As Boolean
			Get
				Return addToRootCheckBox.Checked
			End Get
		End Property

		#End Region


		#Region "Methods"

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			_outline.Title = outlineTitle.Text
			_outline.IsExpanded = outlineExpanded.Checked
			_outline.Flags = PdfBookmarkFlags.None
			If outlineBold.Checked Then
				_outline.Flags = _outline.Flags Or PdfBookmarkFlags.Bold
			End If
			If outlineItalic.Checked Then
				_outline.Flags = _outline.Flags Or PdfBookmarkFlags.Italic
			End If
			_outline.Color = colorDialog1.Color
			If destRadioButton.Checked Then
				If pageNumber.Value > 0 Then
					Dim page As PdfPage = FindPage(CInt(Math.Truncate(pageNumber.Value)) - 1)
					If page IsNot Nothing Then
						_outline.Destination = New PdfDestinationFit(_document, page)
					Else
						DemosTools.ShowWarningMessage("Bookmarks", String.Format("Page {0} is not from this PDF document. Save document and try again.", pageNumber.Value))
						Return
					End If
				End If
			Else
				_outline.Action = _action
			End If
			DialogResult = System.Windows.Forms.DialogResult.OK
			Close()
		End Sub

		Private Function FindPage(index As Integer) As PdfPage
			If _viewer Is Nothing Then
				Return _document.Pages(index)
			End If
			Dim info As ImageSourceInfo = _viewer.Images(index).SourceInfo
			If info.Stream Is _document.SourceStream Then
				Return _document.Pages(info.PageIndex)
			End If
			Return Nothing
		End Function

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = System.Windows.Forms.DialogResult.Cancel
			Close()
		End Sub

		Private Sub button1_Click(sender As Object, e As EventArgs)
			colorDialog1.ShowDialog()
		End Sub

		Private Sub actionRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			editActionButton.Enabled = actionRadioButton.Checked
			pageNumber.Enabled = Not actionRadioButton.Checked
			If actionRadioButton.Checked Then
				If Not _init Then
					If Not EditAction() Then
						destRadioButton.Checked = True
					End If
				End If
			End If
		End Sub

		Private Function EditAction() As Boolean
			Dim form As New ActionEditorForm(_document, _action)
			If form.ShowDialog() = DialogResult.OK Then
				_action = form.Action
				Return _action IsNot Nothing
			End If
			Return False

		End Function

		Private Sub editActionButton_Click(sender As Object, e As EventArgs)
			EditAction()
			If _action Is Nothing Then
				destRadioButton.Checked = True
			End If
		End Sub

		#End Region


	End Class
End Namespace
